﻿using AutoMapper;
using MediatR;
using Microsoft.Extensions.Configuration;
using S.Archives.Api.Application.Command;
using S.Archives.Domain.S.ArchivesModel;
using S.Archives.Infrstructre.InterFace;
using S.Common.helper;
using System.Transactions;

namespace S.Archives.Api.Application.CommandHandlers
{
    public class SheepFileCreatePLCommandHandler : IRequestHandler<SheepFileCreatePLCommand, int>
    {
        private readonly ISheepFileRepository sheepFileRepository;
        private readonly IMapper mapper;
        private readonly IConfiguration configuration;
        public SheepFileCreatePLCommandHandler(ISheepFileRepository sheepFileRepository, IMapper mapper, IConfiguration configuration)
        {
            this.sheepFileRepository = sheepFileRepository;
            this.mapper = mapper;
            this.configuration = configuration;
        }
        /// <summary>
        /// 批量添加羊只档案
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> Handle(SheepFileCreatePLCommand request, CancellationToken cancellationToken)
        {
            int i = 0;
            List<SheepFileModel> sheepmodel = sheepFileRepository.GetAllAsync().Result;
            // 设置事务选项，包括隔离级别为 Serializable
            var transactionOptions = new TransactionOptions
            {
                IsolationLevel = IsolationLevel.Serializable
            };

            using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                try
                {
                    foreach (var item in request.sheepFileModel)
                    {
                        // 判断是否可以配种
                        Parmes parmes = new Parmes();
                        parmes.paternalEITag = item.ParentEarTag;
                        parmes.maternalEITag = item.MotherEarTag;
                      
                        string Gateway = configuration.GetValue<string>("Gateway");

                        var j = HttpClientHelper.PostResponseKey(Gateway+"/ach/api/SheepFileModel/IsOkMatingSpecies", parmes);

                        // 判断耳号是否重复
                        if (sheepmodel.Any(s=>s.EarTag==item.EarTag))
                        {
                            return -1;
                        }
                        //父母三代近亲
                        else if (j!="true")
                        {
                            return -2;
                        }
                        else
                        {
                            //修改耳号状态
                          var a=  HttpClientHelper.PostResponse(Gateway+$"/ggyset/api/Mark/UpdEarTagState?EarTag={item.EarTag}","");
                        }

                        // 处理其他业务逻辑
                    }
                    //添加
                   bool b=await sheepFileRepository.AddRangeAsync(request.sheepFileModel);
                    // 提交事务
                    scope.Complete();
                }
                catch (Exception ex)
                {
                    // 发生异常时回滚事务
                    scope.Dispose();
                    throw ex;
                }
            }

            return 1;
        }


    }
    public class Parmes
    {
        public string paternalEITag { get; set; }
        public string maternalEITag { get; set; }

    }

}
